package com.avic.map.service.impl;

import com.avic.map.common.util.AssertUtil;
import com.avic.map.common.util.ex.BizErrorCode;
import com.avic.map.common.util.ex.BizException;
import com.avic.map.dal.condition.PageQueryMapUavCondition;
import com.avic.map.dal.condition.PageQueryUavResourceListCondition;
import com.avic.map.dal.model.MapUavResource;
import com.avic.map.dal.model.UavResource;
import com.avic.map.service.UavResourceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: huangsheng
 * @date: 2018/9/4
 */
@Service
@Slf4j
public class UavResourceServiceImpl extends AbstractServiceImpl implements UavResourceService {

    /**
     * 分页查询列表
     *
     * @see UavResourceService#itemList(PageQueryUavResourceListCondition)
     */
    @Override
    public List<UavResource> itemList(PageQueryUavResourceListCondition pageQueryUavResourceListCondition) {
        return uavResourceMapper.itemList(pageQueryUavResourceListCondition);
    }

    /**
     * 获取数据总数
     *
     * @see UavResourceService#itemList(PageQueryUavResourceListCondition)
     */

    @Override
    public int queryRecordsTotal(PageQueryUavResourceListCondition pageQueryUavResourceListCondition) {
        return uavResourceMapper.queryRecordsTotal(pageQueryUavResourceListCondition);
    }

    /**
     * 新增资源
     *
     * @see UavResourceService#create(UavResource)
     */
    @Override
    public int create(UavResource uavResource) {

        AssertUtil.isTrue(uavResourceMapper.queryItemCountByUavNumber(uavResource.getUavNumber()) <= 0,
                BizErrorCode.PARAM_ERROR, "无人机编号【" + uavResource.getUavNumber() + "】已经存在");
        try {
            uavResourceMapper.insert(uavResource);
            log.info("创建无人机资源 {}", uavResource.getId());
            return uavResource.getId();
        } catch (DuplicateKeyException de) {
            log.warn("并发无人机舶资源", de.getMessage());
            throw new BizException(BizErrorCode.PARAM_ERROR, "无人机编号【" + uavResource.getUavNumber() + "】已经存在");
        } catch (Exception e) {
            log.error("创建无人机资源发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "创建无人机资源发生错误");
        }
    }

    /**
     * 更新资源
     *
     * @see UavResourceService#update(UavResource)
     */
    @Override
    public void update(UavResource uavResource) {
        log.info("修改无人机资源 {}", uavResource.getId());

        UavResource dbUavResource = uavResourceMapper.queryItemById(uavResource.getId());
        AssertUtil.isTrue(null != dbUavResource, BizErrorCode.PARAM_ERROR, "不存在此数据");

        if (!StringUtils.equals(dbUavResource.getUavNumber(), uavResource.getUavNumber())) {
            AssertUtil.isTrue(uavResourceMapper.queryItemCountByUavNumber(uavResource.getUavNumber()) <= 0,
                    BizErrorCode.PARAM_ERROR, "无人机编号【" + uavResource.getUavNumber() + "】已经存在");
        }
        try {
            uavResourceMapper.update(uavResource);
        } catch (DuplicateKeyException de) {
            log.warn("并发修改无人机资源", de.getMessage());
            throw new BizException(BizErrorCode.PARAM_ERROR, "无人机编号【" + uavResource.getUavNumber() + "】已经存在");
        } catch (Exception e) {
            log.error("修改无人机资源发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "修改无人机资源发生错误");
        }
    }

    /**
     * 删除资源
     *
     * @see UavResourceService#delete(int)
     */
    @Override
    public void delete(int id) {
        try {
            log.info("删除无人机资源 {}", id);
            uavResourceMapper.delete(id);
        } catch (Exception e) {
            log.error("删除无人机资源发生错误 {}", e.getMessage());
            throw new BizException(BizErrorCode.SYSTEM_ERROR, "删除无人机资源的时候发生错误");
        }
    }

    @Override
    public List<UavResource> queryUavTreeRecords() {
        return uavResourceMapper.queryUavTreeRecords();
    }

    @Override
    public List<MapUavResource> queryMapUavListRecords(PageQueryMapUavCondition pageQueryMapUavCondition) {
        return uavResourceMapper.mapUavList(pageQueryMapUavCondition);
    }

    @Override
    public int queryMapUavListRecordCount(PageQueryMapUavCondition pageQueryMapUavCondition) {
        return uavResourceMapper.mapUavListCount(pageQueryMapUavCondition);
    }
}
